<?php
/*
	多平台转接。此处创建多平台，通过转接将消息转发其他平台处理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(isset($_RQ['status'])){
		$params['status like']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','createtime','status','display','title');
	$list=pdo_getslice('core_account_develop',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'第三方平台列表'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'display','title'=>'优先级','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search_one'=>array('name'=>'title','select_name'=>'status','select_title'=>'全部','options'=>'statuss','is_radio'=>true),
	);
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'1'=>'已启用',
		'0'=>'已关闭',
	);
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>trim($post['title']),
			'url'=>trim($post['url']),
			'token'=>trim($post['token']),
			'remark'=>trim($post['remark']),
			'status'=>intval($post['status']),
			'display'=>intval($post['display']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('core_account_develop',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_develop',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_develop',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array(
			'title'=>'',
		);
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置第三方平台信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表页'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'平台名称'),
			array('type'=>'string','name'=>'url','title'=>'平台URL','help'=>'填写第三方平台地址，当没有合适回复规则时，系统会将来自公众平台的消息转发到此地址'),
			array('type'=>'string','name'=>'token','title'=>'平台token'),
			array('type'=>'switch','name'=>'status','title'=>'是否启用'),
			array('type'=>'string','name'=>'display','title'=>'平台优先级','help'=>'填写100以内的数字，数字越大，优先级越高'),
			array('type'=>'text','name'=>'remark','title'=>'备注'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_develop',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_develop',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}